// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  /**
   * 文件上传接口
   * @param {后端接收文件时的变量名} name 
   * @param {文件所在的路径} filePath 
   * @param {form-data的其他附加传参} data 
   */
  upload(name, filePath, data = {}) {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        header: {
          'token': wx.getStorageSync('token') || new String()
        },
        url: `${this.globalData.baseUrl}/file/upload`,
        filePath: filePath,
        name: name,
        formData: data,
        success: result => {
          resolve(result)
        },
        fail: error => {
          reject(error)
        }
      })
    })
  },
  //发起普通请求
  requestPromise(url, method, data = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        header: {
          'Content-Type': 'application/json;charset=UTF-8',
          'token': wx.getStorageSync('token') || new String()
        },
        url: `${this.globalData.baseUrl}${url}`,
        method: method,
        data: data,
        success: result => {
          resolve(result)
        },
        fail: error => {
          reject(error)
        },
      })
    })
  },
  globalData: {
    baseUrl: 'http://127.0.0.1:8080',
    userInfo: null,
  }
})